'use strict';

const { Service } = require('egg');

class SlizeService extends Service {
  async addSlize(params) {
    const result = await this.app.model.Slize.create(params);
    return result.dataValues;
  }
  async deleteSlize(id) {
    const result = await this.app.model.Slize.destroy({ where: { id } });
    return result;
  }
  async updateSlize({ id, slize_name, password, is_admin }) {
    const whereOpt = { id };
    const newUser = {};

    slize_name && Object.assign(newUser, { slize_name });
    password && Object.assign(newUser, { password });
    is_admin && Object.assign(newUser, { is_admin });

    const result = await this.app.model.Slize.update(newUser, {
      where: whereOpt,
    });
    return result;
  }
  async getSlize({ id, slize_name, password, is_admin }) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });
    slize_name && Object.assign(whereOpt, { slize_name });
    password && Object.assign(whereOpt, { password });
    is_admin && Object.assign(whereOpt, { is_admin });

    const result = await this.app.model.Slize.findOne({
      attributes: ['id', 'slize_name', 'password', 'is_admin'],
      where: whereOpt,
    });
    return result;
  }

  async getSlizeList({ pageSize, pageNum }) {
    const offset = (pageNum - 1) * pageSize;
    const { count, rows } = await this.app.model.Slize.findAndCountAll({
      offset,
      limit: pageSize * 1,
    });

    return {
      pageNum,
      pageSize,
      total: count,
      list: rows,
    };
  }
}

module.exports = SlizeService;
